// 定义跟社区食谱相关的路由
const Router = require('koa-router');
// 创建一个新的 Router实例
const router = new Router();

const { getCommentById, postComment } = require('../controllers/index.js');

router.prefix('/comment')

router.get('/getCommentById', async (ctx, next) => {
  const commonId = ctx.query.id
  try {
    const res = await getCommentById(commonId);
    if (res.length) {
      ctx.body = {
        code: 200,
        data: res,
        message: '查询成功'
      }
    } else {
      ctx.body = {
        code: 200,
        data: [],
        message: '暂无数据'
      }
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      data: error,
      message: '服务器异常'
    }
  }
})

router.post('/postComment', async (ctx, next) => {
  const result = ctx.request.body
  try {
    const res = await postComment(result)
    if (res) {
      ctx.body = {
        code: 200,
        message: '评论成功'
      }
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      message: '服务器异常'
    }
  }
})

module.exports = {
  commentRouter: router
};